package com.contory.ssn_lgnd.framework.gl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.StringTokenizer;

import android.util.Log;

import com.contory.ssn_lgnd.framework.FileIO;

public class ESShaderCodeImpoter {
	private Hashtable<String, String> shaderCodeList = new Hashtable<String, String>(); 
	FileIO fileIo;
	
	public ESShaderCodeImpoter(FileIO fileIo){
		this.fileIo = fileIo;
	}
	
	public void addShaderCode(String fileName){
		if(!shaderCodeList.containsKey(fileName))
			shaderCodeList.put(fileName, getFileString(fileIo, fileName));
	}
	
	public String getShaderCode(String fileName){
		return shaderCodeList.get(fileName);
	}
	
	private String getFileString(FileIO fileIo, String fileName){
		BufferedReader in = null;
		StringBuffer buffer = new StringBuffer();
		String result = "";
		String token = "";
		try {
        	in = new BufferedReader(new InputStreamReader(fileIo.readAsset(fileName)));
        	token = "";
        	while((token = in.readLine()) != null)
        		buffer.append(token);
        	result = buffer.toString();
        } catch (IOException e) {
        	result = null;
        } catch (NumberFormatException e) {
        	result = null;
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
            }
        }
		return result;
	}
}
